package com.ruoyi.reportInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportInfo.domain.bo.IndustrialLogisticsCompanyStoreBo;
import com.ruoyi.reportInfo.domain.vo.IndustrialLogisticsCompanyStoreVo;
import com.ruoyi.reportInfo.domain.IndustrialLogisticsCompanyStore;
import com.ruoyi.reportInfo.mapper.IndustrialLogisticsCompanyStoreMapper;
import com.ruoyi.reportInfo.service.IIndustrialLogisticsCompanyStoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物流企业仓库信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-24
 */
@RequiredArgsConstructor
@Service
public class IndustrialLogisticsCompanyStoreServiceImpl implements IIndustrialLogisticsCompanyStoreService {

    private final IndustrialLogisticsCompanyStoreMapper baseMapper;

    /**
     * 查询物流企业仓库信息
     */
    @Override
    public IndustrialLogisticsCompanyStoreVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物流企业仓库信息列表
     */
    @Override
    public TableDataInfo<IndustrialLogisticsCompanyStoreVo> queryPageList(IndustrialLogisticsCompanyStoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyStore> lqw = buildQueryWrapper(bo);
        Page<IndustrialLogisticsCompanyStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物流企业仓库信息列表（统计使用）
     */
    @Override
    public TableDataInfo<IndustrialLogisticsCompanyStoreVo> industrialCompanyStorePageList(IndustrialLogisticsCompanyStoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyStore> lqw = buildQueryWrapper(bo);
        Page<IndustrialLogisticsCompanyStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物流企业仓库信息列表
     */
    @Override
    public List<IndustrialLogisticsCompanyStoreVo> queryList(IndustrialLogisticsCompanyStoreBo bo) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyStore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialLogisticsCompanyStore> buildQueryWrapper(IndustrialLogisticsCompanyStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialLogisticsCompanyStore> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), IndustrialLogisticsCompanyStore::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), IndustrialLogisticsCompanyStore::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), IndustrialLogisticsCompanyStore::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsType()), IndustrialLogisticsCompanyStore::getGoodsType, bo.getGoodsType());
        lqw.eq(bo.getArea() != null, IndustrialLogisticsCompanyStore::getArea, bo.getArea());
        lqw.eq(bo.getCapacity() != null, IndustrialLogisticsCompanyStore::getCapacity, bo.getCapacity());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), IndustrialLogisticsCompanyStore::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialLogisticsCompanyStore::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialLogisticsCompanyStore::getLatitude, bo.getLatitude());
        lqw.eq(bo.getReportCollectDate() != null, IndustrialLogisticsCompanyStore::getReportCollectDate, bo.getReportCollectDate());
        return lqw;
    }

    /**
     * 新增物流企业仓库信息
     */
    @Override
    public Boolean insertByBo(IndustrialLogisticsCompanyStoreBo bo) {
        IndustrialLogisticsCompanyStore add = BeanUtil.toBean(bo, IndustrialLogisticsCompanyStore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物流企业仓库信息
     */
    @Override
    public Boolean updateByBo(IndustrialLogisticsCompanyStoreBo bo) {
        IndustrialLogisticsCompanyStore update = BeanUtil.toBean(bo, IndustrialLogisticsCompanyStore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialLogisticsCompanyStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除物流企业仓库信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
